'''
This module contains functions for AJAX calls.
'''

from django.shortcuts import render_to_response, get_object_or_404
from blogs.models import Blog, Post, Comment
from django.http import HttpResponseRedirect, Http404, HttpResponse
from blogs.utils import comment_utils, get_response_dict
from django.conf import settings
from django.utils.simplejson import *
import blogs.captcha

def comments(request, blog_slug, post_id):
    blog = get_object_or_404(Blog, pk=blog_slug)
    post = get_object_or_404(Post, pk=post_id)
        
    (new_comment_id, comments, comment_form, allowed, user_ip, captcha_error) = \
        comment_utils(request, blog, post)
    
    return render_to_response('blogs/comments.html', get_response_dict(blog, request.user, {
        'post': post,
        'comments': comments,
        'new_comment_id': new_comment_id,
        'comment_form': comment_form,
        'allowed': allowed,
        'user_ip':user_ip,
        'recaptcha_key': settings.RECAPTCHA_PUB_KEY,
        'captcha_error': captcha_error,
        'RECAPTCHA_SETTINGS': JSONEncoder().encode(settings.RECAPTCHA_SETTINGS),
    }))
